package com.zds.hj.poseidon.biz.dispatcher.credit.demo;

import cn.hutool.core.util.RandomUtil;
import com.zds.boot.common.exception.BusinessException;
import com.zds.boot.dispatch.annotation.DispatcherHandler;
import com.zds.boot.dispatch.comm.enums.ModuleNodeTypeEnum;
import com.zds.boot.dispatch.dto.BaseReqModel;
import com.zds.boot.dispatch.dto.BaseResultModel;
import com.zds.hj.poseidon.biz.cba.hpp.domain.credit.service.layout.CreditApplyService;
import com.zds.hj.poseidon.biz.dispatcher.credit.CreditCheckAbsHandler;
import com.zds.hj.poseidon.facade.dto.hpp.credit.CreditInfoEntityDto;
import com.zds.hj.product.facade.constant.ApplicationContainerConstant;
import com.zds.zly.common.enums.HjResultCodeEnum;
import com.zds.zly.common.utils.EmptyChecker;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * @Author duanhao
 * @Description 发送短信验证码
 * @Date 2023/6/6 10:54
 */
@DispatcherHandler(value = "SendMsgHandler", desc = "发送短信验证码", applicationContainer = ApplicationContainerConstant.FRONT_END, nodeType = ModuleNodeTypeEnum.BUSINESS)
@Slf4j
public class SendMsgHandler extends CreditCheckAbsHandler {

    @Resource
    private RedisTemplate redisTemplate;
    @Resource
    private CreditApplyService creditApplyService;

    @Override
    public void creditDoExecute(BaseReqModel params, BaseResultModel resultModel, CreditInfoEntityDto reqData, CreditInfoEntityDto dbData) {
        log.info("验证码入参: {}", reqData.getCode());
        Object o = redisTemplate.opsForValue().get(dbData.getPhone());
        if (EmptyChecker.isEmpty(o)) {
            throw new BusinessException("验证码已失效", HjResultCodeEnum.PARAMS_VALIDATE_FAIL.code());
        }
        String code = String.valueOf(o);
        //检验验证码
        if (!code.equals(reqData.getCode())) {
            throw new BusinessException("验证码不正确", HjResultCodeEnum.PARAMS_VALIDATE_FAIL.code());
        }
        dbData.setCode(reqData.getCode());

    }

    @Override
    public Object preHanding(BaseReqModel params) {
        CreditInfoEntityDto credit = creditApplyService.findCreditOnTransit(params);
        if (EmptyChecker.isEmpty(credit)) {
            throw new BusinessException("授信信息不存在", HjResultCodeEnum.PARAMS_VALIDATE_FAIL.code());
        }
        //发送短信
        String code = RandomUtil.randomNumbers(6);
        redisTemplate.opsForValue().set(credit.getPhone(), code, 5, TimeUnit.MINUTES);
        log.info("预处理发送短信,手机号:{}, 验证码: {}", credit.getPhone(), code);
        return code;
    }

    @Override
    public Object redoPreHanding(BaseReqModel params) {
        CreditInfoEntityDto credit = creditApplyService.findCreditOnTransit(params);
        if (EmptyChecker.isEmpty(credit)) {
            throw new BusinessException("授信信息不存在", HjResultCodeEnum.PARAMS_VALIDATE_FAIL.code());
        }
        String code = RandomUtil.randomNumbers(6);
        redisTemplate.opsForValue().set(credit.getPhone(), code, 5, TimeUnit.MINUTES);
        log.info("重新预处理发送短信,手机号:{}, 验证码: {}", credit.getPhone(), code);
        return code;
    }


    @Override
    public Object paramsFerry(Object o) {
        CreditInfoEntityDto dbDto = (CreditInfoEntityDto) o;
        CreditInfoEntityDto creditInfoEntityDto = new CreditInfoEntityDto();
        creditInfoEntityDto.setCode(dbDto.getCode());
        return creditInfoEntityDto;
    }


}
